<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Manage\Controller;

use User\Api\UserApi as UserApi;
//数据分页
use Think\Page;
/**
 * 用户界面控制器
 * 用户下级用户的管理
 */
class UsersController extends ManageController {
	
	public function _initialize(){
		parent::_initialize();
		$this->assign('left_menu','USERS');
	}

	
	/**
	* 显示全部的用户列表信息
	* @date: 2015-9-12 13:00
	* @author: 刘炎炎
	* @return:
	*/
    public function index(){
    	
    	$map = array(
    		'usertype'=>array('NEQ','0'),
    	);
    	
    	
    	 
    	$keyword = I('post.Keyword');
    	if(($keyword != 0) && ($keyword != '')){
    		$map['customname'] = array('like','%'.$keyword.'%');
    		$this -> assign('keyword',$keyword);
    	}
    	
    	$stime = I('post.ExpiredTime_S');
    	$etime = I('post.ExpiredTime_E');
    	
    	if(($stime != 0) && ($stime != '') && ($etime != 0) && ($etime != '')){
    		$time0 = strtotime($stime);
    		$time1 = strtotime($etime);
    		$map['reg_time'] = array('between',array($time0,$time1));
    		$this -> assign('stime',$stime);
    		$this -> assign('etime',$etime);
    	}
    	
    	$member = M('Member');
    	$count = $member->where($map)->count();
    	$page = new Page($count,20);
    	$show = $page->show();// 分页显示输出

    	// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    	$list = $member->where($map)->order('uid DESC') -> field('uid,nickname,usertype,customname,reg_time,endtime,istest') -> limit($page->firstRow.','.$page->listRows)->select();
    	
    	//获取用户配置信息
    	$userconfigLogic = D('Userconfig','Logic');
    	 
    	foreach ($list as $k=>$v){
    		$userconfig = $userconfigLogic->info(array('uid'=>$v['uid']));
    		$list[$k]['sms'] = $userconfig['sms'] > 0 ? $userconfig['sms'] : 0;
    	}
    	 
    	
    	$this->assign('lists',$list);// 赋值数据集
    	$this->assign('page',$show);// 赋值分页输出
    	$this->display();
    	
    }
    

    /**
    * 添加用户
    * @date: 2015-9-12 14:11
    * @author: 刘炎炎
    * @return:
    */
    public function userAdd($username = '', $password = '', $repassword = '',$email = ''){
    	if(IS_POST){
    		/* 检测密码 */
    		//if($password != $repassword){
    			//$this->error('密码和重复密码不一致！');
    		//}
    	
    		/* 调用注册接口注册用户 */
    		$User   =   new UserApi;
    		$uid    =   $User->register($username, $password, $email);
    		if(0 < $uid){ //注册成功
    			
    			$member = M('Member');
    			
    			$user = array('uid' => $uid, 'nickname' => $username, 'status' => 1);
    			//获取数据信息
    			$data = $member->create();
    			$data['reg_time'] = time();
    			//合并数据
    			$user = array_merge($data,$user);
    			
     			//dump($user);exit;
    			$res = $member->add($user);
    			$agentLogic = D('Agent','Logic');
    			$data = array(
    				'uid' => $res,
    				'name' => $username,
    						
    			);
    			
    		  	$agent = $agentLogic->add($data);
    			//dump($res);exit;
    			if(!$res){
    				$this->error('用户添加失败！');
    			} else {
    				$this->success('代理商添加成功！',U('index'));
    			}
    		} else { //注册失败，显示错误信息
    			$this->error($this->showRegError($uid));
    		}
    	} else {
    		
    		$this->display();
    		
    	}
    }
    
    /**
     * 修改用户信息
     */
    public function userChange(){
    	//如果是提交修改用户信息
    	if(IS_POST){
    		//获取参数
    		$password = I('post.password');
    		$confirmpasswd = I('post.confirmpasswd');
    		
    		if(!empty($password) && $password !== $confirmpasswd){
    			$this->error('您输入的新密码与确认密码不一致');
    		}
    		
    		$uid = I('post.uid',0,'intval');
    		
    		if(0 >= $uid){
    		    $this->error('用户信息错误');
    		}
    		
    		//用户是否修改密码
    		if(!empty($password)){
    			$arr = array('password'=>$password);
    			$Api    =   new UserApi();
    			$res    =   $Api->updateInfo($uid, false, $arr);
    		}
    		
    		$member = M('Member');
    		$data = $member->create();
    		
    		$map = array('uid'=>$uid);
    		//修改用户信息
    		$member->where($map)->save($data);
    		
    		if($member->getError()){
    		    $this->error('修改用户信息错误');
    		}else {
    		    $this->success('修改成功');
    		}
    		
    		
    	}else{
    		$uid = I('get.uid');
    		
    		//判断当前用户id是否是正确的
    		if(0 >= $uid){
    		    $this->error('参数错误');
    		}
    		
    		$member = M('Member');
    		
    		$map = array(
    		    'uid'=>$uid,
    		);
    		
    		$userinfo = $member->where($map)->find();
    		
    		$this->assign('userinfo',$userinfo);
    		
    		$this->display();
    	}
    	
    	
    }
    
    /**
     * 短信设置
     */
//     public function sms(){
//     	$this->display();
//     }
    
    /**
     * 续费
     */
//     public function renew(){
//     	$this->display();
//     }
    
    /**
     * 显示详细信息
     */
    public function detail($uid = 0){
    	
    	//获取用户信息
    	$memberLoic = D('Member','Logic');
    	$userinfo = $memberLoic->info($uid);
    	$this->assign('userinfo',$userinfo);
    	 
    	//获取用户配置信息
    	$userconfigLogic = D('Userconfig','Logic');
    	$userconfig = $userconfigLogic->info(array('uid'=>$uid));
    	$this->assign('userconfig',$userconfig);
    	$sms = $userconfig['sms'] > 0 ? $userconfig['sms'] : 0;
    	$this->assign('sms',$sms);
    	
    	//获取全部的模型信息
    	$ModuleLogic = D('Module','Logic');
    	$modules = $ModuleLogic->allLists(array('status'=>1),true);
    	$this->assign('moudles',$modules);
    	 
    	//用户之前开通的
    	$UserModuleLogic = D('UserModule','Logic');
    	$useropenmodules = $UserModuleLogic->allLists(array('uid'=>$uid),true);
    	$this->assign('useropenMoudles',$useropenmodules);
    	
    	 
    	
    	$this->display();
    }
    
    
    /**
     * 消费记录
     */
    public function records(){
    	
    	$map = array();
    	
    	//搜索关键词
    	$key = trim($_POST['keyword']);
    	if(!empty($key)){
    		$map['desc'] = array('like','%'.$key.'%') ;
    	}
    	
    	$records = D('Records','Model');
    	
    	//类型值
    	$type = intval($_POST['type']);
    	$rtype = $records->getType($type);
    	if(!empty($rtype)){
    		$map['type'] = $type;
    	}
    	
    	//搜索时间
    	
    	$starttime = $_POST['starttime'];
    	$endtime = $_POST['endtime'];
    	//如果同时设置了开始时间和结束时间
    	if(!empty($starttime) && !empty($endtime)){
    	    $starttime = strtotime($starttime);
    	    $endtime = strtotime($endtime);
    	    $map['createtime'] = array(
    	    		array('EGT',$starttime),
    	    		array('ELT',$endtime),
    	    );
    	    //如果只设置了开始时间
    	}else if(!empty($starttime)){
    		$starttime = strtotime($starttime);
    		$map['createtime'] = array(
    				array('EGT',$starttime),
    		);
    		//如果只设置了结束时间
    	}else if(!empty($endtime)){
    		$endtime = strtotime($endtime);
    		$map['createtime'] = array(
    				array('ELT',$endtime),
    		);
    	}
    	
//     	dump($map);exit;
    	
    	$count = $records->where($map)->count();
    	$page = new Page($count,20);
    	$show       = $page->show();// 分页显示输出
    	// 		dump($show);exit();
    	// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    	$list = $records->where($map)->order('id DESC')->limit($page->firstRow.','.$page->listRows)->select();
    	
    	//循环获取消费类型值
    	foreach($list as &$v){
    	   $v['type'] = $records->getType($v['type']);
    	}
    	
//     	dump($list);exit;
    	
    	$this->assign('lists',$list);// 赋值数据集
    	$this->assign('page',$show);// 赋值分页输出
    	
    	$this->assign('left_menu','RECORDS');
    	$this->display();
    	
    }
    
    /**
     * 修改用户绑定的微信账号信息
     */
    public function wechat(){
    	 
    	if(IS_POST){
    		$id = intval($_POST['id']);
    		$uid = intval($_POST['uid']);
    		$weixin = trim($_POST['weixin']);
    
    		if(empty($weixin)){
    			$this->error('未填写微信账号信息');
    		}
    
    		 
    		//获取用户的微信账号信息
    		$WeixinLoic = D('Weixin','Logic');
    		$map = array('id'=>$id);
    		$weixinInfo = $WeixinLoic->where($map)->find();
    
    		$data = array('weixin'=>$weixin);
    
    		if(empty($weixinInfo)){
    			$data['uid'] = $uid;
    			$data['addtime'] = time();
    			$res = $WeixinLoic->add($data);
    		}else {
    			$res = $WeixinLoic->where($map)->save($data);
    		}
    
    		if ($res === false){
    			$this->error('保存出错');
    
    		}else {
    			$this->success('操作成功');
    			exit();
    		}
    
    	}
    	 
    	$uid = intval($_GET['uid']);
    	//查询用户信息
    	$memberLoic = D('Member','Logic');
    	$userinfo = $memberLoic->info($uid);
    	 
    	if(empty($userinfo) ){
    		$this->error('用户信息错误');
    	}
    	 
    	//获取用户的微信账号信息
    	$WeixinLoic = D('Weixin','Logic');
    	$weixin = $WeixinLoic->detail($userinfo['uid']);
    	if(empty($weixin)){
    		$weixin = array('weixin'=>'暂未绑定账号');
    	}
    	//     	echo $WeixinLoic->getLastSql();
    	//     	dump($weixin);dump($userinfo);
    	$this->assign('weixin',$weixin);
    	$this->assign('userinfo',$userinfo);
    	$this->display();
    	 
    }
    
    /**
     * 给用户充值
     */
    public function recharge($uid = 0){
    	
    	if(IS_POST){
    	    $uid = I('post.uid',0,'intval');
    	    $money = I('post.money');
    	    
    	    if($uid <= 0){
    	        $this->error('用户信息错误');
    	    }
    	    
    	    if($money > 0){
    	    	//充值
    	        $memberLogic = D('Member','Logic');
    	        $userinfo = $memberLogic->info($uid);
    	        $res = $memberLogic->where(array('uid'=>$uid))->setInc('money',$money);
    	        
    	        if($res === false){
    	            $this->error('操作出错');
    	        }
    	        
    	        //添加记录
    	        $recordsModel = D('Records','Model');
    	        $res = $recordsModel->recordsAdd($uid,'3',$money,$userinfo['money']+$money,'系统管理员给您充值'.$money.'元');
    	        

    	        if($res === false){
    	        	$this->error('操作出错');
    	        }else {
    	            $this->success('操作成功');
    	        }
    	        exit;
    	    }
    	}

    	//获取用户信息
    	$memberLoic = D('Member','Logic');
    	$userinfo = $memberLoic->info($uid);
    	$this->assign('userinfo',$userinfo);
    	 
    	
        $this->display();
    }
    

}